import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'package:collection/collection.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:scanner/config/wj_color.dart';
import 'package:scanner/main/home/wj_home_page.dart';
import 'package:scanner/main/tab_bar/wj_tab_logic.dart';
import 'package:scanner/models/wj_tab_model.dart';
import 'package:scanner/widgets/common_widgets.dart';

class WJTabPage extends GetView<WJTabLogic> {
  const WJTabPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<WJTabLogic>(
      builder: (logic) {
        return AnnotatedRegion(
          //动态修改当前系统状态栏颜色
          value: controller.currentTabIndex == 0 ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark,
          child:
          Scaffold(
            backgroundColor:
            controller.tabList[controller.currentTabIndex].text == "聊天"
                ? const Color(0xff12121F)
                : WJColor.pageColor,
            body: IndexedStack(
              index: logic.currentTabIndex,
              children: logic.tabList.map((e) => _getTabViewChild(e)).toList(),
            ),
            bottomNavigationBar: Container(
              padding: EdgeInsets.only(top: 10.w),
              color:
              controller.tabList[controller.currentTabIndex].text == "聊天"
                  ? const Color(0xff12121F)
                  : Colors.white,
              child: SafeArea(
                top: false,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: logic.tabList.mapIndexed((index, e) => _tabItem(index)).toList(),
                ),
              ),
            ),
          )
        );
      },
    );
  }

  Widget _tabItem(int index) {
    var tabModel = controller.tabList[index];
    bool isSelected = index == controller.currentTabIndex;
    return Expanded(
      child: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () => controller.tapTab(index),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Image.asset(isSelected ? tabModel.resP : tabModel.resN, width: 24.w, height: 24.w).paddingOnly(bottom: 5.w),
            WJCommonWidgets.getText(
              tabModel.text,
              textColor:
              (controller.tabList[index].text == "聊天" && isSelected)
                  ? WJColor.appColor
                  : isSelected
                  ? WJColor.colorFF22
                  : WJColor.colorFF9F,
              fontSize: 12.sp,
              fontWeight: FontWeight.bold,
            ),
          ],
        ),
      ),
    );
  }

  Widget _getTabViewChild(WJTabModel tabModel) {

    if(tabModel.text == "首页"){
      return WJHomePage();
    }
    return Container();
  }

}
